package com.terabits.smartriver.controller;

import com.terabits.smartriver.constants.result.BaseResp;
import com.terabits.smartriver.constants.result.ResultStatus;
import com.terabits.smartriver.mapper.DeviceMapper;
import com.terabits.smartriver.meta.po.Device;
import com.terabits.smartriver.service.BoxService;
import com.terabits.smartriver.service.DeviceService;
import com.terabits.smartriver.util.PageData;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class DeviceController extends BaseController {
    @Resource
    private DeviceService deviceService;
    @Autowired
    private DeviceMapper deviceMapper;

    @ApiOperation(value = "查询 设备", tags = {"设备管理"})
    @RequestMapping(value = "/device-mgt/device/get", method = RequestMethod.GET)
    public BaseResp getDevice() {
        PageData pageData = this.getPageData();
        List devices = deviceService.getDevice(pageData);
        if (devices != null && devices.size() != 0) {
            return new BaseResp(ResultStatus.SUCCESS, devices);
        }
        return new BaseResp(ResultStatus.error_record_not_found);
    }

    @ApiOperation(value = "查询 设备,去重", tags = {"设备管理"})
    @RequestMapping(value = "/device-mgt/device/getDevice", method = RequestMethod.GET)
    public BaseResp getDevices() {
        PageData pageData = this.getPageData();
        List<Device> devices = deviceService.getDevices(pageData);
        if (devices != null && devices.size() != 0) {
            return new BaseResp(ResultStatus.SUCCESS, devices);
        }
        return new BaseResp(ResultStatus.error_record_not_found);
    }

    @ApiOperation(value = "通过项目查询设备", tags = {"设备管理"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectNum", value = "项目编号"),
    })
    @RequestMapping(value = "/device-mgt/device/getDeviceByProjectNum", method = RequestMethod.GET)
    public BaseResp getDeviceByProjectNum(String projectNum) {
        Device device = new Device();
        device.setProjectNum(projectNum);
        List<Device> devices = deviceMapper.select(device);
        if (devices.size() > 0) {
            return new BaseResp(ResultStatus.SUCCESS, devices);
        }
        return new BaseResp(ResultStatus.error_record_not_found);
    }

    @ApiOperation(value = "二维码查询 设备", tags = {"设备管理"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "device_num", value = "设备编号"),
    })
    @RequestMapping(value = "/device-mgt/device/getOne", method = RequestMethod.GET)
    public BaseResp getOneDevice(String device_num) {
        Device oneDevice = deviceService.getOneDevice(device_num);
        if (oneDevice != null) {
            return new BaseResp(ResultStatus.SUCCESS, oneDevice);
        }
        return new BaseResp(ResultStatus.error_record_not_found);
    }

    @ApiOperation(value = "添加 设备", tags = {"设备管理"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "device", value = "设备pojo对象"),
    })
    @RequestMapping(value = "/device-mgt/device/add", method = RequestMethod.POST)
    public BaseResp addDevice(Device device) {
        int res = deviceService.addDevice(device);
        if (res == -1) {
            return new BaseResp(ResultStatus.error_duplicated_data);
        }
        return new BaseResp(ResultStatus.SUCCESS, res);
    }

    @ApiOperation(value = "修改 设备", tags = {"设备管理"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "device", value = "设备pojo对象"),
    })
    @RequestMapping(value = "/device-mgt/device/update", method = RequestMethod.POST)
    public BaseResp updateDevice(Device device) {
        return new BaseResp(ResultStatus.SUCCESS, deviceService.updateDevice(device));
    }

    @ApiOperation(value = "删除 设备", tags = {"设备管理"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "设备编号"),
    })
    @RequestMapping(value = "/device-mgt/device/delete", method = RequestMethod.POST)
    public BaseResp deleteDevice(Integer id) {
        return new BaseResp(ResultStatus.SUCCESS, deviceService.deleteDevice(id));
    }

    @ApiOperation(value = "查看 设备保养", tags = {"设备管理-保养"})
    @RequestMapping(value = "/device-mgt/maintenance/get", method = RequestMethod.GET)
    public BaseResp getDeviceMaintenance() {
        return new BaseResp(ResultStatus.SUCCESS, deviceService.getDeviceMaintenance(this.getPageData()));
    }


}